package com.zmn.plat.admin.controller.message.group;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zmn.common.dto2.ResponseDTO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto.StatusDTO;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.admin.controller.message.MsgTemplateBaseController;
import com.zmn.plat.business.interfaces.message.MsgTemplateGroupBService;
import com.zmn.plat.model.entity.message.MsgTemplateGroup;

/**
 * 描述: 消息管理模板组修改Controller
 *
 * @auther tanghongjun
 * @since 2019/10/21 16:36
 */
@Controller
@RequestMapping("/msg/temp/group/")
public class MsgTemplateGroupModifyController extends MsgTemplateBaseController {

    private Logger logger = LoggerFactory.getLogger(MsgTemplateGroupModifyController.class);

    @Resource
    private MsgTemplateGroupBService msgTemplateGroupBService;

    /**
     * region 新增/修改
     *
     * @param msgTemplateGroup
     *            模板组新增修改实体
     * @return ModelAndView 执行结果
     */
    @RequestMapping(value = "addEdit")
    @RequiresPermissions("/msg/temp/group/addEditView.action")
    @ResponseBody
    public ResponseDTO addEdit(HttpServletRequest request, @RequestBody MsgTemplateGroup msgTemplateGroup) {
        String lable = "新增模板组";
        String realName = getMcStaff().getRealName();

        try {
            if (msgTemplateGroup.getGroupId() == null) {
                lable = "新增模板组";
                int flag = msgTemplateGroupBService.insert(msgTemplateGroup, realName);
                if (flag == GlobalConsts.NO) {
                    return ResponseDTO.fail("同一平台同一模块模板组名称不能重复！");
                }
                return ResponseDTO.success(true,"新增模板组成功！");
            } else {
                lable = "修改模板组";
                int flag = msgTemplateGroupBService.updateByKey(msgTemplateGroup, realName);
                if (flag == GlobalConsts.NO) {
                    return ResponseDTO.fail("同一平台同一模块模板组名称不能重复！");
                }
                return ResponseDTO.success(true,"修改模板组成功！");
            }
        } catch (Exception ex) {
            logger.error(String.format("[zmn]新增/修改模板组失败,servProductVO:[%s],ex:[%s]", ex, ex.getMessage()));
            return ResponseDTO.fail(lable + "失败！");
        }
    }

    /**
     * 描述:修改模板组状态
     *
     * @param ids
     *            模板组ID组成的字符串 eg: productId1,productId2
     * @param status
     *            修改的状态
     * @return: ResultDTO 执行结果
     * @auther: tanghongjun
     * @since: 2018/11/5 22:39
     */
    @RequestMapping(value = "updateStatus")
    @RequiresPermissions("/msg/temp/group/updateStatus.action")
    @ResponseBody
    public ResponseDTO updateStatus(HttpServletRequest request, HttpServletResponse response,
        @RequestParam(value = "ids", defaultValue = "") String ids, @RequestParam(value = "status", defaultValue = "0") int status) {

        if (StringUtil.isBlank(ids)) {
            return ResponseDTO.fail("请选择要更改状态的模板组！");
        }

        List<Integer> groupIds = StringUtil.convertStrsToList(ids);
        StatusDTO dto = new StatusDTO();
        dto.setIds(groupIds);
        dto.setStatus(status);
        dto.setUpdater(getMcStaff().getRealName());
        msgTemplateGroupService.updateStatusBatch(dto);

        return ResponseDTO.success(true,"操作成功！");
    }

    /**
     * 功能描述 :修改模板组排序
     *
     * @param request
     * @param response
     * @param groupId
     * @param sort
     * @return com.zmn.common.dto.ResultDTO
     * @author tanghongjun
     * @since 2019年10月22日11:53:15
     * @modifier
     */
    @RequestMapping(value = "updateSort")
    @ResponseBody
    public ResponseDTO updateSort(HttpServletRequest request, HttpServletResponse response,
        @RequestParam(value = "id", defaultValue = "0") Integer groupId, @RequestParam(value = "sort", defaultValue = "0") Integer sort) {

        if (groupId == 0) {
            return ResponseDTO.fail("请选择要更改排序的模板组！");
        }
        if (sort == 0) {
            return ResponseDTO.fail("请设置模板组排序值！");
        }

        msgTemplateGroupService.updateSortByKey(groupId, sort, getMcStaffRealName());
        return ResponseDTO.success(true,"操作成功！");
    }
}
